<?php 
  /**
   * Kontrollbase
   *
   * An open source MySQL monitoring and analytics application
   *
   * @package Kontrollbase
   * @author Matt Reid
   * @copyright Copyright (c) 2009 Matt Reid, Kontrollbase LLC
   * @license http://kontrollsoft.com/kontrollbase/userguide/general-LICENSE.php
   * @link http://kontrollbase.com
   */

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Model_commands extends Model
{

  function __construct()
  {
    $this->Model_commands();
  }
  
  function Model_commands()
  {
    parent::Model();
  }

  function delete_data($id) {
    log_message('debug', "model_commands: delete_data called");
    log_message('debug', "model_commands: id: $id");
    $id = $this->db->escape_str($id);

    $dbr = $this->load->database('write', TRUE);
    $sql0="DELETE from `command_objects` WHERE id = '$id' LIMIT 1;";
    log_message('debug', "$sql0");

    $this->db->trans_start();
    $this->db->query($sql0);
    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE) {
      log_message('debug', "model_commands delete_data transaction failed");
      $this->db->trans_off();
      return 1;
    } 
    else {
      log_message('debug', "model_commands delete_data transaction success");
      $this->db->trans_off();
      return 0;
    }
  }


  function update_data(
                       $id,
                       $field,
                       $value) {

    log_message('debug', "model_commands: update_data called");
    log_message('debug', "model_commands: id: $id, field: $field, value: $value");
    $id = $this->db->escape_str($id);
    $field = $this->db->escape_str($field);
    $value = $this->db->escape_str($value);
    
    $dbr = $this->load->database('write', TRUE);
    $sql0="UPDATE `command_objects` SET $field = '$value' where id = '$id'";
    log_message('debug', "$sql0");

    $this->db->trans_start();
    $this->db->query($sql0);
    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE) {
      log_message('debug', "model_commands updated_data transaction failed");
      $this->db->trans_off();
      return 1;
    } 
    else {
      log_message('debug', "model_commands updated_data transaction success");
      $this->db->trans_off();
      return 0;
    }
  }

  function add_data() {    
    $dbr = $this->load->database('write', TRUE);
    $sql0="INSERT INTO `command_objects` (
`id` ,
`command_objects_name` ,
`command_objects_desc` ,
`command_objects_command` 
)
VALUES (
NULL, 
'name',
'description',
'command');";

    $this->db->trans_start();
    $this->db->query($sql0);
    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE) {
      show_error('Could not add new row. Transaction failed.');
      $this->db->trans_off();
      return 1;
    } 
    else {
      $this->db->trans_off();
      return 0;
    }
  }

  function get_data() {
    log_message('debug', "model_commands::get_data function starting.");
    $dbr = $this->load->database('read', TRUE);
    $sql = "select * from command_objects order by id";
    $query = $dbr->query($sql);
    if($query->num_rows() > 0) {
      log_message('debug', "get_data:: returning data");
      return $query->result_array();
    }
    else {
      log_message('debug', "get_data:: no data to display");
    }
  }
}
?>